package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.PublishConstants;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PWaitMattersBo;
import com.ruoyi.system.domain.vo.PWaitMattersVo;
import com.ruoyi.system.domain.PWaitMatters;
import com.ruoyi.system.mapper.PWaitMattersMapper;
import com.ruoyi.system.service.IPWaitMattersService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 待办事项Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-28
 */
@RequiredArgsConstructor
@Service
public class PWaitMattersServiceImpl implements IPWaitMattersService {

    private final PWaitMattersMapper baseMapper;

    /**
     * 查询待办事项
     */
    @Override
    public PWaitMattersVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询待办事项列表
     */
    @Override
    public TableDataInfo<PWaitMattersVo> queryPageList(PWaitMattersBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PWaitMatters> lqw = buildQueryWrapper(bo);
        Page<PWaitMattersVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询待办事项列表
     */
    @Override
    public List<PWaitMattersVo> queryList(PWaitMattersBo bo) {
        LambdaQueryWrapper<PWaitMatters> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PWaitMatters> buildQueryWrapper(PWaitMattersBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PWaitMatters> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, PWaitMatters::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getWaitType()), PWaitMatters::getWaitType, bo.getWaitType());
        lqw.eq(bo.getQuestId() != null, PWaitMatters::getQuestId, bo.getQuestId());
        lqw.eq(bo.getAppointTime() != null, PWaitMatters::getAppointTime, bo.getAppointTime());
        lqw.eq(bo.getAdviserId() != null, PWaitMatters::getAdviserId, bo.getAdviserId());
        lqw.eq(bo.getUserId() != null, PWaitMatters::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增待办事项
     */
    @Override
    public Boolean insertByBo(PWaitMattersBo bo) {
        PWaitMatters add = BeanUtil.toBean(bo, PWaitMatters.class);
        add.setUserId(LoginHelper.getUserId());
        add.setType(PublishConstants.COMMON_NO);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 修改待办事项
     */
    @Override
    public Boolean updateByBo(PWaitMattersBo bo) {
        PWaitMatters update = BeanUtil.toBean(bo, PWaitMatters.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PWaitMatters entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除待办事项
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insertByFont(PWaitMatters bo) {
        bo.setUserId(LoginHelper.getUserId());
        baseMapper.insert(bo);
    }
}
